package com.example.studentportrait.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.studentportrait.domain.ResponseResult;
import com.example.studentportrait.domain.dto.ListAssessmentTypeDto;
import com.example.studentportrait.domain.entity.AssessmentType;
import com.example.studentportrait.domain.vo.PageVo;
import com.example.studentportrait.enums.AppHttpCodeEnum;
import com.example.studentportrait.exception.SystemException;
import com.example.studentportrait.mapper.AssessmentTypeMapper;
import com.example.studentportrait.service.AssessmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.baomidou.mybatisplus.extension.toolkit.Db.*;

@Service("assessmentTypeService")
public class AssessmentTypelmpl extends ServiceImpl<AssessmentTypeMapper, AssessmentType> implements AssessmentTypeService {
    @Autowired
    private AssessmentTypeMapper assessmentTypeMapper;
    //获取题型列表
    @Override
    public ResponseResult assessentTypelist(int pageNum, int size, ListAssessmentTypeDto listAssessmentTypeDto) {
        LambdaQueryWrapper<AssessmentType> queryWrapper = new LambdaQueryWrapper<>();

        // 先检查 DTO 是否为 null
        if (listAssessmentTypeDto != null) {
            // 根据名称、描述和类型进行查询
            queryWrapper.like(StringUtils.hasText(listAssessmentTypeDto.getName()), AssessmentType::getAssessmentName, listAssessmentTypeDto.getName());
            queryWrapper.like(StringUtils.hasText(listAssessmentTypeDto.getDescription()), AssessmentType::getAssessmentDescription, listAssessmentTypeDto.getDescription());
            queryWrapper.eq(StringUtils.hasText(listAssessmentTypeDto.getMajor()), AssessmentType::getMajor, listAssessmentTypeDto.getMajor());
        }

        // 分页查询
        Page<AssessmentType> page = new Page<>(pageNum, size);
        IPage<AssessmentType> assessmentTypePage = assessmentTypeMapper.selectPage(page, queryWrapper);

        // 获取查询结果
        List<AssessmentType> assessmentTypeList = assessmentTypePage.getRecords();

        // 返回分页结果
        PageVo pageVo = new PageVo(assessmentTypeList, assessmentTypePage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    //增加题型
    @Override
    @Transactional
    public ResponseResult addAssessmentType(AssessmentType assessmentType) {
        System.out.println(assessmentType);
        if (!StringUtils.hasText(assessmentType.getAssessmentDescription()) || assessmentType.getAssessmentName() == null) {
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
//       新增tag
        save(assessmentType);
        return ResponseResult.okResult();
    }
    //更新题型
    @Override
    public ResponseResult<AssessmentType> updateAssessmentType(AssessmentType assessmentType) {
        if ( assessmentType.getId() == null) {
            throw new SystemException(AppHttpCodeEnum.ID_NOT_NULL);
        }

        // 构建 UpdateWrapper
        UpdateWrapper<AssessmentType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", assessmentType.getId());

        // 只更新非 null 的字段
        if (assessmentType.getAssessmentName() != null) {
            updateWrapper.set("assessment_name", assessmentType.getAssessmentName());
        }
        if (assessmentType.getAssessmentDescription() != null) {
            updateWrapper.set("assessment_description", assessmentType.getAssessmentDescription());
        }
        if (assessmentType.getMajor() != null) {
            updateWrapper.set("major", assessmentType.getMajor());
        }
        if (assessmentType.getUpdateTime() != null) {
            updateWrapper.set("update_time", assessmentType.getUpdateTime());
        }
        System.out.println(assessmentType);
        // 执行更新操作
        boolean updated = update(updateWrapper);
        if (!updated) {
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return ResponseResult.okResult(assessmentType); // 返回更新后的对象
    }

    //删除题型
    @Override
    public ResponseResult deleteAssessmentType(Integer id) {
        if (Objects.nonNull(id) && id > 0) {
            boolean removed = removeById(id);
            if (!removed) {
                throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
            }
        } else {
            throw new SystemException(AppHttpCodeEnum.ID_NOT_NULL);
        }
        return ResponseResult.okResult();
    }

    //批量删除题型
    @Override
    public ResponseResult deleteAssessmentTypes(List<Integer> ids) {
        // 使用 MyBatis-Plus 的 removeByIds 方法来批量删除
        boolean result = removeByIds(ids);

        if (result) {
            return ResponseResult.okResult("题型测试删除成功");
        } else {
            throw new SystemException(AppHttpCodeEnum.ANNOUNCEMENT_DELETE_FAIL);
        }
    }
}

